package chapter16;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ControlCircle2 extends JFrame{
	/**
	 * 实现一个简单的监听Demo
	 */
	private static final long serialVersionUID = 1L;
	private JButton jbtEnlarge = new JButton("Enlarge");
	private JButton jbtSharink = new JButton("Sharink");
	private CirclePanel canvas = new CirclePanel();
	
	public ControlCircle2(){
		JPanel panel = new JPanel();
		panel.add(jbtEnlarge);
		panel.add(jbtSharink);
		
		this.add(canvas, BorderLayout.CENTER);
		this.add(panel, BorderLayout.SOUTH);
		
		jbtEnlarge.addActionListener(new EnlargeListener());
		jbtSharink.addActionListener(new SharinkListener());
	}
	
	public static void main(String[] agrs){
		JFrame frame = new ControlCircle2();
		frame.setTitle("ControlCircle2");
		frame.setSize(200, 200);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	class EnlargeListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			canvas.enlarge();
		}
	}
	
	class SharinkListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			canvas.sharink();
		}
	}
	
	class CirclePanel extends JPanel{
		/**
		 * 显示动画效果
		 */
		private static final long serialVersionUID = 1L;
		private int radius = 5;
		
		public void enlarge(){
			radius = 2 * radius;
			repaint();
		}
		
		public void sharink(){
			radius--;
			repaint();
		}
		
		protected void paintComponent(Graphics g){
			super.paintComponent(g);
			
			g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius, 2 * radius, 2 * radius);
		}
	}

}
